﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsGame4
{
    class Enemy_DestroyState : State
    {
        private static Enemy_DestroyState instance;

        private Enemy_DestroyState()
        {
            base.Name = "DestroyState";
        }

        public override void enter(AIObject aiObject)
        {
            //System.Console.WriteLine("destroystate");
            AIEnemy aiEnemy = (AIEnemy)aiObject;
            Enemy enemy = (Enemy)aiEnemy.Enemy;
            GameInfo gameInfo = GameInfo.getInstance();
            
            if (enemy.Blinking == false)
            {
                // Decrement lives
                enemy.Lives--;
                // Put to the initial point
                int rowVictim = gameInfo.World.getNumRow(enemy.Position.Y);
                int columnVictim = gameInfo.World.getNumColumn(enemy.Position.X);
                enemy.Position = new Position2D(enemy.InitialPosition.X, enemy.InitialPosition.Y);
                int initialRow = gameInfo.World.getNumRow(enemy.InitialPosition.Y);
                int initialColumn = gameInfo.World.getNumColumn(enemy.InitialPosition.X);
                //gameInfo.World.get(initialRow, initialColumn).Obj = enemy;
                //gameInfo.World.get(rowVictim, columnVictim).Obj = null;
                enemy.Movement = null;
                enemy.Route = null;
                EventEmitter.getInstance().registerMessage(0, aiEnemy.Id, aiEnemy.Id, Message.MessageType.msg_create);

                if (enemy.Lives <= 0)
                {
                    enemy.Lives = 0;
                    // Remove object
                    gameInfo.removeObject(enemy.Id, enemy.Type);
                }
                else
                {
                    // Create star
                    gameInfo.createRandomStar(enemy.Level - enemy.InitalLevel);
                    // Remove level
                    enemy.Level = enemy.InitalLevel;
                }
            }
            else
            {
                EventEmitter.getInstance().registerMessage(0, aiEnemy.Id, aiEnemy.Id, Message.MessageType.msg_stop);
            }
            //System.Console.WriteLine("exitdestroystate");
        }

        public static Enemy_DestroyState getInstance()
        {
            if (instance == null)
                instance = new Enemy_DestroyState();
            return instance;
        }

        public override void execute(AIObject aiObject)
        {
            //System.Console.WriteLine("executedestroystate");
            AIEnemy aiEnemy = (AIEnemy)aiObject;
            Enemy enemy = (Enemy)aiEnemy.Enemy;
            GameInfo gameInfo = GameInfo.getInstance();
        }

        public override void exit(AIObject aiObject)
        {
        }

        public override Boolean onMessage(AIObject aiObject, Message message)
        {
            Boolean correct = false;
            switch (message.Type)
            {
                case Message.MessageType.msg_create:
                    {
                        aiObject.StateMachine.changeState(Enemy_InitialState.getInstance());
                        correct = true;
                        break;
                    }
                case Message.MessageType.msg_stop:
                    {
                        aiObject.StateMachine.changeState(Enemy_StopState.getInstance());
                        correct = true;
                        break;
                    }
            }
            return correct;
        }
    }
}
